//
//  FLLMineViewController.m
//  FLLGodOfCookery
//
//  Created by LL.F on 16/8/27.
//  Copyright © 2016年 LL.F. All rights reserved.
//

#import "FLLMineViewController.h"
#import "FLLMacro.h"
#import "Masonry.h"
#import "FLLMineTableViewCell.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"
#import "FLLMineModel.h"
#import "Data.h"
#import "SecondData.h"
#import "FLLLoginViewController.h"
#import "FLLSettingViewController.h"
#import "ZWYNetTool.h"
#import "LoginData.h"

@interface FLLMineViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UIView *navigationView;
@property (nonatomic, strong) UIButton *setButton;
@property (nonatomic, strong) UITableView *mineTableView;
@property (nonatomic, strong) UIView *mineHeaderView;
@property (nonatomic, strong) FLLMineModel *fllMineModel;
@property (nonatomic, strong) UIImageView *userImageView;
@property (nonatomic, strong) UILabel *userNameLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) UILabel *worksLabel;
@property (nonatomic, strong) UILabel *attentionLabel;
@property (nonatomic, strong) UILabel *fansLabel;
@end

@implementation FLLMineViewController
- (void)viewWillAppear:(BOOL)animated
{

    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = YES;
//    self.tabBarController.tabBar.hidden = NO;
}
- (void)viewWillDisappear:(BOOL)animated
{

    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
//    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 网络请求
    [self netWorkingPost];
    // 创建tableView
    [self createTableView];
    // 设置头视图
    [self setHeaderView];
    [self createHeaderView];
    
    

    
}
// 自定义view充当导航栏
- (void)createHeaderView {
    self.navigationView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    [self.view addSubview:_navigationView];
    self.navigationView.backgroundColor = [UIColor clearColor];
    self.setButton = [UIButton buttonWithType:UIButtonTypeCustom];
    
    [self.setButton setImage:[UIImage imageNamed:@"setButton"] forState:UIControlStateNormal];
    self.setButton.adjustsImageWhenHighlighted = NO;
    [_navigationView addSubview:_setButton];
    [self.setButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.with.mas_equalTo(35);
        make.right.mas_equalTo(-20);
        make.centerY.mas_equalTo(0).mas_offset(20);
    }];
    [_setButton addTarget:self action:@selector(didClickedSettingButton:) forControlEvents:UIControlEventTouchUpInside];
    
    UILabel *titleLabel = [[UILabel alloc] init];
    [self.navigationView addSubview:titleLabel];
    titleLabel.text = @"我的";
    titleLabel.textColor = [UIColor whiteColor];
    titleLabel.font = [UIFont systemFontOfSize:17 * kWidthScale];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(0);
        make.height.mas_equalTo(_setButton.mas_height);
        make.centerY.mas_equalTo(_setButton.mas_centerY);
    }];

}
#pragma mark -- POST请求
- (void)netWorkingPost
{
    
    [ZWYNetTool POST:@"http://api.izhangchu.com/" andBody:@"methodName=UserWelcome&version=4.40&" andBodyStyle: ZWYBodyString andHeader:@{@"Content-Type":@"application/x-www-form-urlencoded"} andResponse:ZWYJSON andSuccessBlock:^(id result) {
        
        self.fllMineModel = [FLLMineModel modelWithDic:result];
        self.block();
        [self.mineTableView reloadData];
        
    } andFailureBlock:^(NSError *error) {
        
    }];
    
    
  
}
#pragma mark -- createMethodBySelf
- (void)createTableView
{

    self.mineTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT -49)];
    _mineTableView.delegate = self;
    _mineTableView.dataSource = self;
    _mineTableView.bounces = NO;
    [self.view addSubview:_mineTableView];
    // 注册:
    [_mineTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    [_mineTableView registerNib:[UINib nibWithNibName:@"FLLMineTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"mineCell"];

    
    
}
// 设置按钮
- (void)didClickedSettingButton:(UIBarButtonItem *)button
{
    FLLSettingViewController *setVC  = [[FLLSettingViewController alloc] init];
    [self.navigationController pushViewController:setVC animated:YES];
}
- (void)setHeaderView
{
    __weak FLLMineViewController *weakSelf = self;
    self.block = ^(){
    
        [weakSelf.userImageView sd_setImageWithURL:[NSURL URLWithString:weakSelf.loginModel.data.headphoto] placeholderImage:[UIImage imageNamed:@"userHeadImage-1"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
            
        }];
        weakSelf.userNameLabel.text = weakSelf.loginModel.data.nickname;
        weakSelf.worksLabel.text = [NSString stringWithFormat:@"作品(%zd)", weakSelf.loginModel.data.tongji_post];
        weakSelf.attentionLabel.text = [NSString stringWithFormat:@"关注(%zd)", weakSelf.loginModel.data.tongji_follow];
        weakSelf.fansLabel.text = [NSString stringWithFormat:@"粉丝(%zd)", weakSelf.loginModel.data.tongji_be_follow];

        
        weakSelf.contentLabel.text = nil;
        
    };
    self.mineHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 300)];
    // 背景图片
    UIImageView *mineImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_HEIGHT, 260)];
//    mineImageView.backgroundColor = [UIColor redColor];
    mineImageView.image = [UIImage imageNamed:@"myBackgroundImage"];
    [_mineHeaderView addSubview:mineImageView];
    
    
    // 黑色背景
    UIView *blackView = [[UIView alloc] init];
    blackView.backgroundColor = [UIColor colorWithWhite:0.000 alpha:0.298];
    [_mineHeaderView addSubview:blackView];
    [blackView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(-40);
        make.height.mas_equalTo(_mineHeaderView.mas_height).multipliedBy(0.4);
    }];
    // 厨师图标
    self.userImageView = [[UIImageView alloc] init];
    //    userImageView.backgroundColor = [UIColor yellowColor];
    self.userImageView.image = [UIImage imageNamed:@"userHeadImage-1"];
    self.userImageView.layer.cornerRadius = 30.0f;
    self.userImageView.layer.masksToBounds = YES;
    [_mineHeaderView addSubview:self.userImageView];
    [self.userImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.height.width.mas_equalTo(60);
        make.bottom.mas_equalTo(0).mas_offset(- 140);
    }];
    // 用户名
    self.userNameLabel = [[UILabel alloc] init];
    //    userName.backgroundColor = [UIColor orangeColor];
//    self.userNameLabel.text = @"子涵";
    self.userNameLabel.textColor = [UIColor whiteColor];
    [blackView addSubview:self.userNameLabel];
    [self.userNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.mas_equalTo(100);
        make.height.mas_equalTo(30);
    }];
    
    // label
    self.contentLabel = [[UILabel alloc] init];
    self.contentLabel.text = @"每个吃货都是有头有脸的, 登录下吧";
    self.contentLabel.textColor = [UIColor whiteColor];
    //    label.backgroundColor = [UIColor blueColor];
    [blackView addSubview:self.contentLabel];
    [self.contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(20);
        make.height.mas_equalTo(30);
        make.right.mas_equalTo(-20);
        make.bottom.mas_equalTo(- 50);
    }];
    
    // 作品关注粉丝条
    UIView *bottomView = [[UIView alloc]init];
    bottomView.backgroundColor = [UIColor whiteColor];
    [_mineHeaderView addSubview:bottomView];
    [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(0);
        make.top.mas_equalTo(blackView.mas_bottom);
    }];
    // "作品"
    self.worksLabel = [[UILabel alloc] init];
     self.worksLabel.font = [UIFont systemFontOfSize:14 * kWidthScale];
    self.worksLabel.textAlignment = 1;
    [bottomView addSubview:self.worksLabel];
    [self.worksLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(bottomView.mas_width).multipliedBy(0.3333);
    }];
    // "关注"
    self.attentionLabel = [[UILabel alloc] init];
    self.attentionLabel.textAlignment = 1;
    self.attentionLabel.font = [UIFont systemFontOfSize:14 * kWidthScale];
    [bottomView addSubview:self.attentionLabel];
    [self.attentionLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_equalTo(0);
        make.left.mas_equalTo(self.worksLabel.mas_right);
        make.width.mas_equalTo(self.worksLabel.mas_width);
    }];
    // "粉丝"
    self.fansLabel = [[UILabel alloc] init];
    self.fansLabel.font = [UIFont systemFontOfSize:14 * kWidthScale];
    self.fansLabel.textAlignment = 1;
    [bottomView addSubview:self.fansLabel];
    [self.fansLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.mas_equalTo(0);
        make.left.mas_equalTo(self.attentionLabel.mas_right);
    }];
    
    self.mineTableView.tableHeaderView = _mineHeaderView;
    
    
}

#pragma mark -- tableViewDatasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{

    if (section == 1) {
        return 3;
    } if (section == 5) {
        return 0;
    }
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{

    return 6;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    
    FLLMineTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"mineCell"];
    cell.listLabel.font = [UIFont systemFontOfSize:14 * kWidthScale];
    if (indexPath.section == 0) {
        cell.listLabel.text = @"我的消息";
    } else if (indexPath.section == 1) {
        SecondData *secondDataModel = self.fllMineModel.data.data[indexPath.row];
        cell.listLabel.text = secondDataModel.text;
        [cell.alertImageView sd_setImageWithURL:[NSURL URLWithString:secondDataModel.image] placeholderImage:[UIImage new] completed:nil];
       
    } else if (indexPath.section == 2) {
        cell.listLabel.text = @"我的缓存";
    } else if (indexPath.section == 3) {
    
        cell.listLabel.text = @"我的喜欢";
    } else if (indexPath.section == 4) {
    
        cell.listLabel.text = @"邀请好友";
    }
    return cell;

}
// 区头视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{

    UIView *sectionHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 10)];
    sectionHeaderView.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.93 alpha:1.00];
    return sectionHeaderView;
    
}
// 区头高度
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{

    return 10 * kHeightScale;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return 44 * kHeightScale;
}
// 点击cell
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

    if (indexPath.section == 1) {
        FLLLoginViewController *login = [[FLLLoginViewController alloc] init];
        [self.navigationController pushViewController:login animated:YES];

    }
    
}
- (UIStatusBarStyle)preferredStatusBarStyle
{
    // 状态栏显示为亮色(要想显示为暗色就用UIStatusBarStyleDefault)
    return UIStatusBarStyleLightContent;
    
}
- (void)setLoginModel:(FLLLoginModel *)loginModel
{

    _loginModel = loginModel;
    [self.mineTableView reloadData];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
